package edu.tgc.service.qrtz;

import java.util.TimeZone;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.ScheduleBuilder;
import org.quartz.Trigger;

import edu.tgc.entity.qrtz.QrtzTrigger;

public class CronTriggerScheduleBuilder implements TriggerScheduleBuilder {

    @Override
    public ScheduleBuilder<CronTrigger> build(QrtzTrigger qrtzTrigger) {
        CronScheduleBuilder triggerBuilder = CronScheduleBuilder.cronSchedule(qrtzTrigger.getExpression()); // cron表达式
        triggerBuilder.inTimeZone(TimeZone.getTimeZone(qrtzTrigger.getTimeZone())); // 时区
        // 设置触发机制 默认 SimpleTrigger.MISFIRE_INSTRUCTION_SMART_POLICY
        switch (qrtzTrigger.getMisfireInstruction()) {
        case CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING:
            triggerBuilder.withMisfireHandlingInstructionDoNothing();
            break;
        case CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW:
            triggerBuilder.withMisfireHandlingInstructionFireAndProceed();
            break;
        case Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY:
            triggerBuilder.withMisfireHandlingInstructionIgnoreMisfires();
            break;
        default:
            break;
        }
        return triggerBuilder;
    }
}
